home *** CD-ROM | disk | FTP | other *** search
- /* Utils.cs - Contains the Utils class which has a collection of
- * functions that can simplify development elsewhere.
- *
- * See individual function comments for more details.
- */
-
- using System;
- using System.Drawing;
- using System.IO;
- using System.Windows.Forms;
- using System.Runtime.InteropServices;
- using System.Threading;
-
- namespace GameClasses {
- public class Utils {
- // External function to play sounds
- [DllImport("winmm.dll")]
- public static extern long PlaySound(String lpszName, long hModule,
- long dwFlags);
-
- // Static Variable .config file's contents
- public static System.Collections.Specialized.NameValueCollection
- Config = System.Configuration.ConfigurationSettings.AppSettings;
-
- // Error-handling LoadImage function
- public static Image LoadImage(String strFileName) {
- Image imgLoad = null;
- try {
- imgLoad = Image.FromFile(strFileName);
- } catch (FileNotFoundException ex) {
- MessageBox.Show("Please check that the following file " +
- "exists:\n\n" + ex.Message + "\n\nCorrect the file " +
- "referenced in the config file.", "File Not Found!");
- Application.Exit();
- }
-
- return imgLoad;
- }
-
- // PlaySound simple pass-through function. Removes dependency for
- // other classes to reference winmm.DLL.
- public static void PlaySound(String strFileName) {
- ThreadedSound snd = new ThreadedSound(strFileName);
- Thread worker = new Thread(new ThreadStart(snd.Play));
- worker.Start();
- }
-
- /* While this thread does speed up the games, it also causes a very
- unnatural delay - note how the sounds continue to play after the game is shutdown.
- The solution, simply replace the calls to this funciton with the sound functions
- used with DirectSound. DirectSound is covered in chapter six. */
- class ThreadedSound {
- public string strFileName;
-
- public ThreadedSound(string file) {
- strFileName = file;
- }
-
- public void Play() {
- //Console.WriteLine("Playing: " + strFileName);
- PlaySound(strFileName, 0, 0);
- }
- }
- }
- }
-